package com.jch.learn.config;

import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * Nacos相关接口处理
 */
@Slf4j
public class NacosManager {

    private static volatile NacosManager instance = null;

    private NamingService namingService = null;

    private ConfigService configService = null;

    private NacosManager(String serverAddr) {
        if (instance != null) throw new RuntimeException("NacosManager has already been initialized");
        try {
            namingService = NamingFactory.createNamingService(serverAddr);
            configService = ConfigFactory.createConfigService(serverAddr);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    private NacosManager(Properties properties) {
        if (instance != null) throw new RuntimeException("NacosManager has already been initialized");
        try {
            namingService = NamingFactory.createNamingService(properties);
            configService = ConfigFactory.createConfigService(properties);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    public static void create(String serverAddr) {
        if (instance == null) {
            synchronized (NacosManager.class) {
                if (instance == null) {
                    instance = new NacosManager(serverAddr);
                }
            }
        }
    }

    public static void create(Properties properties) {
        if (instance == null) {
            synchronized (NacosManager.class) {
                if (instance == null) {
                    instance = new NacosManager(properties);
                }
            }
        }
    }

    public static NacosManager getInstance() {
        if (instance == null) throw new RuntimeException("NacosManager has not been initialized");
        return instance;
    }

    /**
     * 向nacos注册服务
     * @param serviceName 服务名
     * @param groupName 组名
     * @param servicePort 服务端口
     * @return 是否注册成功
     */
    public boolean register(String serviceName,String groupName,int servicePort) {
        try {
            String ip = InetAddress.getLocalHost().getHostAddress();
            Instance instance = new Instance();
            instance.setIp(ip);
            instance.setPort(servicePort);
            namingService.registerInstance(serviceName,groupName,instance);
            return true;
        } catch (UnknownHostException | NacosException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 向nacos取消注册
     * @param serviceName 服务名
     * @param groupName 组名
     * @param servicePort 服务端口
     * @return 是否取消成功
     */
    public boolean deregister(String serviceName,String groupName,int servicePort) {
        try {
            String ip = InetAddress.getLocalHost().getHostAddress();
            namingService.deregisterInstance(serviceName,groupName,ip,servicePort);
            return true;
        } catch (UnknownHostException | NacosException e) {
            throw new RuntimeException(e);
        }
    }

}
